package com.androidhuman.example.CreateActivity;

//import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.RatingBar;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

import android.widget.AutoCompleteTextView;
import android.widget.ArrayAdapter;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;



public class CreateActivity extends		TabActivity
							implements	CompoundButton.OnCheckedChangeListener {
	/** Called when the activity is first created. */
	
	private Button 		callActivityButton;
	private Button 		openWebPageButton;
	private TabHost 	mTabHost;
	
	private	EditText	editText;
	private Spinner		spinner;
	private TextView	textView;
	private Button		getTextButton;
    
	private RadioGroup	radioGroup;
	private TextView 	radioChecker;
	
	private CheckBox	checkBox;
	private TextView	checkBoxChecker;
	
	private ProgressBar pgB;
	private Button		toggleIndeterminate;
	private Button		pincB,pdecB,sincB,sdecB;
	
	private SeekBar		seekBar;
	private TextView	seekBarText;
	
	private RatingBar	rtnBar;
	private TextView	rtnbarText;
	
	private DatePicker	dp;
	private TimePicker 	tp;
	private TextView	dateText,timeText;
	
	private AutoCompleteTextView 	autoComplete;
	private ArrayList<String>		list;
	private ArrayAdapter<String>	adapter;
	
	ArrayList<MyItem>	arItem;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
                
        mTabHost = getTabHost();
        mTabHost.setup();
        
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
        						.setContent(R.id.tabs1)
        						.setIndicator("Tab1"));        
        
        mTabHost.addTab(mTabHost.newTabSpec("tab2")
								.setContent(R.id.tabs2)
								.setIndicator("Tab2"));
        
        mTabHost.addTab(mTabHost.newTabSpec("tab3")
								.setContent(R.id.tabs3)
								.setIndicator("Tab3"));
        
        mTabHost.addTab(mTabHost.newTabSpec("tab4")
								.setContent(R.id.tabs4)
								.setIndicator("Tab4"));
       
        mTabHost.addTab(mTabHost.newTabSpec("tab5")
								.setContent(R.id.tabs5)
								.setIndicator("Tab5"));
        
        mTabHost.addTab(mTabHost.newTabSpec("tab6")
								.setContent(R.id.tabs6)
								.setIndicator("Tab6"));
        
        mTabHost.addTab(mTabHost.newTabSpec("tab7")
								.setContent(R.id.tabs7)
								.setIndicator("Tab7"));
        
       	autoComplete = (AutoCompleteTextView)findViewById(R.id.autoComplete);
    	list = new ArrayList<String>();    	
    	
    	list.add("Android");
    	list.add("Andromeda");
    	list.add("Breezy Badger");
    	list.add("Cupcake");
    	list.add("Donut");
    	list.add("Dapper Drake");
    	list.add("Edge Eft");
    	list.add("Eclair");
    	list.add("Froyo");
    	list.add("Gusty Gibbon");
    	list.add("Hoary Hedgehog");
    	list.add("Hardy Heron");
    	list.add("Intrepid ibex");
    	list.add("Karmic Koala");
    	
    	adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,list);
    	autoComplete.setAdapter(adapter);
    	
    	
    	MyItem mi;
    	arItem = new ArrayList<MyItem>();    	
    	
    	mi = new MyItem(R.drawable.icon, "삼성 노트북");
    	arItem.add(mi);
    	
    	mi = new MyItem(R.drawable.icon, "엘지 세탁기");
    	arItem.add(mi);
    	
    	mi = new MyItem(R.drawable.icon, "대우 마티즈");
    	arItem.add(mi);
    	
    	MyListAdapter MyAdapter = new MyListAdapter(this,R.layout.icontext, arItem); 
    	
    	ListView MyList;
    	MyList = (ListView)findViewById(R.id.listView);
    	MyList.setAdapter(MyAdapter);    	    	
    	
        editText = (EditText)findViewById(R.id.editText);
        textView = (TextView)findViewById(R.id.textview);
        
        checkBoxChecker = (TextView)findViewById(R.id.cbChecker);
        checkBox = (CheckBox)findViewById(R.id.checkBox);
        
        checkBox.setOnCheckedChangeListener(this);
        
        callActivityButton = (Button)findViewById(R.id.callActivity);
        callActivityButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		// Intent intent = new Intent(CreateActivity.this, NewActivity.class);
        		Intent intent = new Intent("com.androidhuman.action.TEST_ACTION");
        		//startActivity(intent);
        		startActivityForResult(intent,0);
        	}
        });
        
        openWebPageButton = (Button)findViewById(R.id.callActivity2);
        openWebPageButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		Intent intent = new Intent(Intent.ACTION_VIEW);
        		intent.setData(Uri.parse("http://www.google.co.kr"));
        		startActivity(intent);
        	}        	
        });
        
        getTextButton = (Button)findViewById(R.id.button);
        getTextButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		textView.setText(editText.getText().toString());
        	}
        });
        
        spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        	@Override
        	public void onItemSelected(AdapterView<?> parent,
        					View view, int position, long id) {
        		String str = (String)parent.getItemAtPosition(position);
        		textView.setText(str);
        	}
        	
        	@Override
        	public void onNothingSelected(AdapterView<?> parent) {
        		//TODO Auto-generated method stud
        	}
        });
        
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        	@Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
        		radioChecker = (TextView)findViewById(R.id.radioChecker);
            	switch(checkedId) {
            		case R.id.radio1:
            			radioChecker.setText("Android Selected");
            			break;
            		case R.id.radio2:
            			radioChecker.setText("iPhone Selected");
            			break;
            	}
            }
        });
        
        pgB = (ProgressBar)findViewById(R.id.pgB);        
        toggleIndeterminate = (Button)findViewById(R.id.toggleIndeterminate);
        toggleIndeterminate.setOnClickListener(new OnClickListener() {
        	@Override      	
        	public void onClick(View v) {
        			pgB.setIndeterminate(!pgB.isIndeterminate());
        	}
        });
        
        pincB = (Button)findViewById(R.id.increasePrimaryProgress);
        pincB.setOnClickListener(new OnClickListener() { 
        	@Override
        	public void onClick(View v) {
       			pgB.incrementProgressBy(1);
        	}
        });
        
        pdecB = (Button)findViewById(R.id.decreasePrimaryProgress);
        pdecB.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
       			pgB.incrementProgressBy(-1);
        	}
        });
        
        sincB = (Button)findViewById(R.id.increaseSecondaryProgress);
        sincB.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
       			pgB.incrementSecondaryProgressBy(1);
        	}
        });
        
        sdecB = (Button)findViewById(R.id.decreaseSecondaryProgress);
        sdecB.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
       			 pgB.incrementSecondaryProgressBy(-1);
        	}
        });
 
        seekBarText = (TextView)findViewById(R.id.seekbartext);
        seekBar = (SeekBar)findViewById(R.id.seekbar);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {        	
        	@Override
        	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        		seekBarText.setText("SeekBar at : " + progress);
        	}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub				
			}
        });
        
        rtnbarText = (TextView)findViewById(R.id.ratingbartext);
        rtnBar = (RatingBar)findViewById(R.id.ratingbar);
        rtnBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        	@Override
        	public void onRatingChanged(RatingBar rtnBar, float rating, boolean fromUser) {
        		rtnbarText.setText("Rating : " + rating + "/5.0");
        	}
        });
        
        dp = (DatePicker)findViewById(R.id.DatePicker);
        dateText = (TextView)findViewById(R.id.dateView);
        dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
        	    new DatePicker.OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            	dateText.setText(year + "년 " + (monthOfYear+1) + "월 " + dayOfMonth + "일");
            	//dateText.setText(String.format("%d/%d/%d", year, monthOfYear + 1, dayOfMonth));
            }
        });
        
        tp = (TimePicker)findViewById(R.id.TimePicker);
    	timeText = (TextView)findViewById(R.id.timeView);
    	tp.setOnTimeChangedListener(new OnTimeChangedListener() {
    		@Override
    		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    			timeText.setText(hourOfDay + "시 " + minute + "분");
    		}
    	});
    }
    
	class MyItem {    		
		int Icon;
		String Name;
		
		MyItem(int aIcon, String aName) {
			Icon = aIcon;
			Name = aName;
		}
	}
	
	class MyListAdapter extends BaseAdapter {
		Context				maincon;
		LayoutInflater 		Inflater;
		ArrayList<MyItem>	arSrc;
		int 				layout;
		
		public MyListAdapter(Context context, int alayout, ArrayList<MyItem> aarSrc) {
			maincon		= context;
			Inflater	= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			arSrc		= aarSrc;
			layout		= alayout;
		}
		
		//추상메소드를 구현해야됨
        @Override
        public int getCount() {//getCount는 데이터의 사이즈를 리턴하면됨
        	// TODO Auto-generated method stub

        	return arSrc.size();
        }
        
        @Override
        public Object getItem(int position) {
        	// TODO Auto-generated method stub
        	return arSrc.get(position).Name;
        	//getItem은 그 position의 값을 리턴하면됨
        }
        
        @Override
        public long getItemId(int position) {
        	// getItemId는 position을 리턴해주면됨
        	// TODO Auto-generated method stub
        		return position;
        }           

        @Override
        //이 부분은 리스트 각항목 하나하나를 만드는 부분임
        public View getView(int position, View convertView, ViewGroup parent) {
        	// TODO Auto-generated method stub
        	
        	final int pos = position;

        	// 첫번째는 convertView가 null이여서 inflate를 통해서
        	// 마지막인자는 Whether the inflated hierarchy should be attached to the root parameter?
        	// 해석 못했음

        	if(convertView == null){
        		//layout은 R.layout.icontext parent는 뷰그룹인 리스트뷰를 뜻함
        		convertView = Inflater.inflate(layout, parent, false);
        	}

        	// 이미지뷰를 세팅하고
        	ImageView img = (ImageView)convertView.findViewById(R.id.imgLstView);
        	img.setImageResource(arSrc.get(position).Icon);
        	
        	// 텍스브튜도 세팅
        	TextView txt = (TextView)convertView.findViewById(R.id.textLstView);
        	txt.setText(arSrc.get(position).Name);

        	// 버튼도 세팅함
        	Button btn = (Button)convertView.findViewById(R.id.btnLstView);
        	btn.setOnClickListener(new OnClickListener() {
        		@Override
        		public void onClick(View v) {
        			//TODO Auto-generated method stub
        			String str = arSrc.get(pos).Name + "를 주문합니다.";
        			Toast.makeText(maincon, str, Toast.LENGTH_SHORT).show();
        		}
        	});                 
        	
        	return convertView;//위 과정이 리스트뷰에 getCount만큼 반복됨
        }
	}
    
	@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		//TODO Auto-generated method stub
		
		checkBoxChecker.setText("Checked : " + checkBox.isChecked());        		
	}
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 0) {
			if(resultCode == TabActivity.RESULT_OK) {
				TextView tv 		= (TextView)findViewById(R.id.result);
				StringBuilder sb 	= new StringBuilder();
				sb.append("숫자 : ")
				  .append(data.getIntExtra("data_int", -1))
				  .append(" 문자열 : ")
				  .append(data.getStringExtra("data_string"));
				tv.setText(sb.toString());
			}
		}
	}
}